
$(function () {
    setTimeout('$("html").removeClass("js")', 1);
    permissionMgrFuc.searchPermissionValidation();
    permissionMgrFuc.addPermissionValidation();
    permissionMgrFuc.updatePermissionValidation();   
    
    $("#search_btn").click(function(){
        //清空pageIndex
        $("input[name=currentPage]").val(1);
    });
    
    $("#clear_cache").click(function(){
    	smoke.confirm("确认清除权限缓存？", function(e){
            if (e){
                var url = "permission/clearPermissionCode.action";               
                $.post(url,null,function(data){                    
                	smoke.signal(data.msg,2000);
                },"json");
            }else{}
        }, {
            ok: "确认",
            cancel: "取消",
            reverseButtons: true
        });
    });
});

/*显示增加权限信息 开始*/
var addPermissionRoleMap = new Map();
$("#add_permission_btn").click(function() {
	add_bind_role($("#add_role_name").val());
    $("#add_permission_win").modal('show');
});
$("#add_role_search_btn").click(function(){
	add_bind_role($("#add_role_name").val());
});
function add_bind_role(roleName){
	$("#add_start_set").empty();
	$.post("role/findRole.action",{roleName:roleName},function(json){
		if (json&&json.success) {
		  $.each(json.result.data, function(i,item){
			  if(!addPermissionRoleMap.containsKey(item.roleId.toString())){
				  $("#add_start_set").append("<label class='checkbox'><input type='checkbox' value='"+item.roleId+"'/><font title='"+item.roleDescription+"'>"+item.roleName+"</font></label>");
			  }
		  });
		}else {
        	alert(json?json.msg:"请求失败！");
        }
	});
}

$("#add_role_right_btn").click(function(){
	$("#add_start_set>label>:checkbox:checked").each(function(i,item){
		if(!addPermissionRoleMap.containsKey(item.value)){
			addPermissionRoleMap.put(item.value,item.value);
			$("#add_end_set").prepend($(item).parent());
		}
	});
});

$("#add_role_left_btn").click(function(){
	$("#add_end_set>label>:checkbox:checked").each(function(i,item){
		if(addPermissionRoleMap.containsKey(item.value)){
			addPermissionRoleMap.remove(item.value);
			$("#add_start_set").prepend($(item).parent());
		}
	});
});
/*显示修改权限信息 结束*/

/*增加权限信息方法 开始*/
$("#add_permission_confirm").click(function () {	
    var  show_add_permission_name_status  = $.trim($("#show_add_permission_name_status").html());
    //表单数据验证通过
    if ($('#add_permission_form').validate().form() && 0===show_add_permission_name_status.length) {
        $.post("permission/add.action",
            {
        		permissionName:$.trim($("#add_permission_name").val()),
        		permissionCode:$.trim($("#add_permission_code").val()),
        		permissionUrl:$.trim($("#add_permission_url").val()),
                permissionDescription:$.trim($("#add_permission_description").val()),
                roleIds:addPermissionRoleMap.keys().join(",")
            },
            function (json) {
            	if (json&&json.success) {
                    //关闭增加权限信息窗口
                    $("#add_permission_win").modal('hide');
                    //重置表单数据
                    document.getElementById("add_permission_form").reset();
                    //重新刷新查询列表
                    refreshPage();
                } else {
                	alert(json?json.msg:"请求失败！");
                }
       });
    }
});
/*增加权限信息方法 结束*/


/*显示修改权限信息 开始*/
var updatePermissionRoleMap = new Map();
function show_update_permission(id) {
    $.post("permission/detail.action",{permissionId:id},
		function (json) {
	    	if (json&&json.success) {
	        	var permission=json.result.data;
	            $("#update_permission_id").val(permission.permissionId);
	            $("#update_permission_name").val(permission.permissionName);
	            $("#update_permission_code").val(permission.permissionCode);
	            $("#update_permission_url").val(permission.permissionUrl);
	            $("#update_permission_description").val(permission.permissionDescription);
	            //角色信息
	            updatePermissionRoleMap.clear();
	            $("#update_end_set").empty();
	            $.each(json.result.roles, function(i,item){
	            	updatePermissionRoleMap.put(item.roleId.toString(),item.roleId.toString());
	            	//首次加载初始化已有角色
	            	$("#update_end_set").append("<label class='checkbox'><input type='checkbox' value='"+item.roleId+"'/><font title='"+item.roleDescription+"'>"+item.roleName+"</font></label>");
	            });	            
	            update_bind_role($("#update_role_name").val());
	            
	            $("#update_permission_win").modal('show');
	    	}else{
	    		alert(json?json.msg:"请求失败！");
	        }
    });
}

function update_bind_role(roleName){
	$("#update_start_set").empty();
	$.post("role/findRole.action",{roleName:roleName},function(json){
		if (json&&json.success) {
		  $.each(json.result.data, function(i,item){
			  if(!updatePermissionRoleMap.containsKey(item.roleId.toString())){
				  $("#update_start_set").append("<label class='checkbox'><input type='checkbox' value='"+item.roleId+"'/><font title='"+item.roleDescription+"'>"+item.roleName+"</font></label>");
			  }
		  });
		}else {
        	alert(json?json.msg:"请求失败！");
        }
	});
}
$("#update_role_search_btn").click(function(){
	update_bind_role($("#update_role_name").val());
});
$("#update_role_right_btn").click(function(){
	$("#update_start_set>label>:checkbox:checked").each(function(i,item){
		if(!updatePermissionRoleMap.containsKey(item.value)){
			updatePermissionRoleMap.put(item.value,item.value);
			$("#update_end_set").prepend($(item).parent());
		}
	});
});

$("#update_role_left_btn").click(function(){
	$("#update_end_set>label>:checkbox:checked").each(function(i,item){
		if(updatePermissionRoleMap.containsKey(item.value)){			
			updatePermissionRoleMap.remove(item.value);
			$("#update_start_set").prepend($(item).parent());
		}
	});
});

$("#update_permission_cancel").click(function(){
	$("#update_permission_win").modal('hide');
});
/*显示修改权限信息 结束*/



/*提交修改权限信息 开始*/
$("#update_permission_confirm").click(function () {
    if ($('#update_permission_form').validate().form()) {        
        $.post("permission/edit.action",
            {
        		permissionId: $("#update_permission_id").val(),
        		permissionName:$.trim($("#update_permission_name").val()),
        		permissionCode: $("#update_permission_code").val(),
        		permissionUrl: $("#update_permission_url").val(),
        		permissionDescription:$.trim($("#update_permission_description").val()),
        		roleIds:updatePermissionRoleMap.keys().join(",")
            },
            function (json) {
            	if (json&&json.success) {
                    //关闭修改权限窗口
                    $("#update_permission_win").modal('hide');
                    //重置表单数据
                    //刷新权限管理查询列表页面
                    refreshPage();

                } else{
                	alert(json?json.msg:"请求失败！");
                }
            });
    }
});
/*提交修改权限信息 结束*/

/*显示详细权限信息 开始*/
function show_detail_permission(id) {
    $.post("permission/detail.action",{permissionId:id},
		function (json) {
    		if (json&&json.success) {
	        	var permission=json.result.data;
	            $("#detail_permission_id").val(permission.permissionId);
	            $("#detail_permission_name").val(permission.permissionName);
	            $("#detail_permission_code").val(permission.permissionCode);
	            $("#detail_create_time").val(new Date(permission.createTime).format());                
	            $("#detail_update_time").val(new Date(permission.updateTime).format());
	            $("#detail_permission_description").val(permission.permissionDescription);
	            
	            $("#detail_role_set").empty();
	            $.each(json.result.roles, function(i,item){
	            	$("#detail_role_set").append("<span>"+item.roleName+"</span><br/>");
	            });
	            
	            $("#detail_permission_win").modal('show');
        	}else{
        		alert(json?json.msg:"请求失败！");
            }
	        
    	});
}

$("#detail_permission_confirm").click(function(){
	$("#detail_permission_win").modal('hide');
});
/*显示详细权限信息 结束*/

// 函数定义
permissionMgrFuc = {
	searchPermissionValidation:function () {
        reg_validator = jQuery('#search_form').validate({
            errorClass:'error',
            validClass:'valid',
            highlight:function (element) {
                $(element).closest('div').addClass("f_error");
            },
            unhighlight:function (element) {
                $(element).closest('div').removeClass("f_error");
            },
            errorPlacement:function (error, element) {
                $(element).closest('div').append(error);
            },
            rules:{
            	//permissionId:{digits:true}
            },
            messages:{
            	//permissionId:{digits:"权限编号必须是整数！"}
            },
            invalidHandler:function (form, validator) {
                $.sticky("您的查询条件有问题，请仔细检查！", {autoclose:5000, position:"top-right", type:"st-error" });
            }
        })
    },
    // validator 创建权限
    addPermissionValidation:function () {
        reg_validator = jQuery('#add_permission_form').validate({
            errorClass:'error',
            validClass:'valid',
            highlight:function (element) {
                $(element).closest('div').addClass("f_error");
            },
            unhighlight:function (element) {
                $(element).closest('div').removeClass("f_error");
            },
            errorPlacement:function (error, element) {
                $(element).closest('div').append(error);
            },
            rules:{
            	add_permission_name:{ required:true},
            	add_permission_code:{ required:true},
            	add_permission_description:{ required:true }
            },
            messages:{
            	add_permission_name:{ required:"权限名称不能为空！"},
            	add_permission_code:{ required:"权限码不能为空！"},
            	add_permission_description:{required:"权限描述不能为空！"}
            },
            invalidHandler:function (form, validator) {
                $.sticky("您的表单填写的有问题，请仔细检查！", {autoclose:5000, position:"top-right", type:"st-error" });
            }
        })
    },
    // validator 修改权限
    updatePermissionValidation:function () {
        reg_validator = jQuery('#update_permission_form').validate({
            errorClass:'error',
            validClass:'valid',
            highlight:function (element) {
                $(element).closest('div').addClass("f_error");
            },
            unhighlight:function (element) {
                $(element).closest('div').removeClass("f_error");
            },
            errorPlacement:function (error, element) {
                $(element).closest('div').append(error);
            },
            rules:{
            	update_permission_name:{ required:true},
            	update_permission_code:{ required:true},
            	update_permission_description:{ required:true }
            },
            messages:{
            	update_permission_name:{ required:"权限名称不能为空！"},
            	update_permission_code:{ required:"权限码不能为空！"},
            	update_permission_description:{required:"权限描述不能为空！"}
            },
            invalidHandler:function (form, validator) {
                $.sticky("您的表单填写的有问题，请仔细检查！", {autoclose:5000, position:"top-right", type:"st-error" });
            }
        })
    }
};


